package com.ykdl.member.kid.person;

import java.util.List;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.KidApp;
import com.ykdl.member.http.Network;
import com.ykdl.member.kid.adapters.FavoriteAdapter;
import com.ykdl.member.kid.adapters.PostAdapter;
import com.ykdl.member.kid.adapters.TopicAdapter;
import com.ykdl.member.kid.beans.FavoriteBean;
import com.ykdl.member.kid.beans.FavoriteListBean;
import com.ykdl.member.kid.beans.TopicAndPostBean;
import com.ykdl.member.kid.beans.TopicAndPostListBean;
import com.ykdl.member.kid.beans.TopicBean;
import com.ykdl.member.kid.beans.TopicListBean;
import com.ykdl.member.kid.circle.TopicAndResultActivity;
import com.ykdl.member.kid.fittool.BaseActivity;
import com.ykdl.member.kid.widget.RefreshListView;

public class PostListActivity extends BaseActivity implements OnClickListener, OnItemClickListener{

	private static final String TAG = PostListActivity.class.getSimpleName();
	
	private TextView mTxtCount;
	private RefreshListView mListView;
	
	private int start_id = 0;
	private int cursor = 0;
	private int count = 50;
	private PostAdapter mAdapter;
	private ProgressDialog mProgressDialog;
	private PostListTag mListTag;
	
	private long post_actor_id;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_post);
		post_actor_id = getIntent().getIntExtra("post_actor_id", 0);
		initView();
		mProgressDialog = mProgressDialog.show(this, null, getString(R.string.loading_data));
		mListTag = new PostListTag();
		Network.getInstance().requestPostList(start_id, cursor, count, post_actor_id, mListTag);
		
	}
	
	private void initView(){
		
		findViewById(R.id.btnBack).setOnClickListener(this);
		mTxtCount = (TextView) findViewById(R.id.txtCount);
		mListView = (RefreshListView) findViewById(R.id.listView);
		mAdapter = new PostAdapter(this);
		mListView.setAdapter(mAdapter);
		mListView.setMoreClick(this);
		mListView.setOnItemClickListener(this);
		
	}
	
	private class PostListTag implements ITag<TopicAndPostListBean> {

		@Override
		public void refreshUI(TopicAndPostListBean t, DataState ds) {
			if (mProgressDialog != null) mProgressDialog.dismiss();
			if (t != null) {
				List<TopicAndPostBean> beans = t.getList();
				mAdapter.addTopic(beans);
				mAdapter.notifyDataSetChanged();
				if (t.getNext_cursor() == t.getTotal_number()) {
					mListView.setMoreButtoIsGon(true);
				} else {
					cursor = t.getNext_cursor();
					
				}
				mTxtCount.setText(getString(R.string.favorite_count, t.getTotal_number()));
			} else {
				Toast.makeText(PostListActivity.this, "暂无数据", 1).show();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			if (mProgressDialog != null) mProgressDialog.dismiss();
			if (TextUtils.isEmpty(parseError.getErrMsg())) {
				return false;
			} else {
				Toast.makeText(PostListActivity.this, "暂无数据", 1).show();
				return true;
			}
		}

		@Override
		public void refreshCancel() {
			mProgressDialog.dismiss();
		}
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnBack:
			finish();
			break;
		case R.id.iask_root_loadmore:
			Network.getInstance().requestPostList(start_id, cursor, count, post_actor_id, mListTag);
		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		TopicAndPostBean post = (TopicAndPostBean) mAdapter.getItem(position - 1);
		Intent intent = new Intent(this, TopicAndResultActivity.class);
		intent.putExtra(TopicAndResultActivity.STR_TOPIC_ID, String.valueOf(post.getTopic_id()));
		intent.putExtra(TopicAndResultActivity.STR_GROUP_ID, String.valueOf(post.getSource_id()));
		intent.putExtra(TopicAndResultActivity.STR_POST_ID, String.valueOf(post.getPost_id()));
		startActivity(intent);
	}
	
}
